//
//  HWEveryDayTaskCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/21.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit

class HWEveryDayTaskCell: UITableViewCell {
    var logoImageView:UIImageView!
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var rightButton:UIButton!
    var didClickRightButton:((_:HWEveryDayTaskDayModel)->())?
    var model:HWEveryDayTaskDayModel?{
        didSet{
            logoImageView.kf.setImage(with: URL(string: model?.icon ?? ""))
            titleLabel.text = model?.name
            contentLabel.text = model?.point
            if model?.type == "12" {
                if model?.status == "2"{
                    rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#F7F7F7").cgColor
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
                    rightButton.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
                    rightButton.setTitle("已完成", for: .normal)
                    rightButton.isEnabled = false
                }else if model?.status == "1" {
                    rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#F87706").cgColor
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#FFFFFF"), for: .normal)
                    rightButton.backgroundColor = .colorWithHexString(hexString: "#F87706")
                    rightButton.setTitle("领取", for: .normal)
                    rightButton.isEnabled = true
                }else {
                    rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#F87706").cgColor
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#FFFFFF"), for: .normal)
                    rightButton.backgroundColor = .colorWithHexString(hexString: "#F87706")
                    rightButton.setTitle("签到", for: .normal)
                    rightButton.isEnabled = true
                }
            } else {
                if model?.status == "2"{
                    rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#F7F7F7").cgColor
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
                    rightButton.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
                    rightButton.setTitle("已完成", for: .normal)
                    rightButton.isEnabled = false
                }else if model?.status == "1" {
                    rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#F87706").cgColor
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#FFFFFF"), for: .normal)
                    rightButton.backgroundColor = .colorWithHexString(hexString: "#F87706")
                    rightButton.setTitle("领取", for: .normal)
                    rightButton.isEnabled = true
                }else {
                    rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#F87706").cgColor
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#FFFFFF"), for: .normal)
                    rightButton.backgroundColor = .colorWithHexString(hexString: "#F87706")
                    rightButton.setTitle("去完成", for: .normal)
                    rightButton.isEnabled = true
                }
            }
        }
    }
    
    var indexPath:IndexPath!
    class func cellWithTableView(tableView:UITableView) -> (HWEveryDayTaskCell) {
        let reUse:String = "HWEveryDayTaskCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWEveryDayTaskCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWEveryDayTaskCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        logoImageView = UIImageView()
        contentView.addSubview(logoImageView)
        
        titleLabel = HWLabel.createLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        contentView.addSubview(contentLabel)
        
        rightButton = UIButton(type: .custom)
        contentView.addSubview(rightButton)
        rightButton.layer.cornerRadius = 14*widthScale
        rightButton.titleLabel?.font = UIFont.systemFont(ofSize: 13*widthScale, weight: .semibold)
        rightButton.addTarget(self, action: #selector(rightButtonClick(_:)), for: .touchUpInside)
        rightButton.layer.borderWidth = 1
    }
    
    func initFrame() -> Void {
        logoImageView.snp.makeConstraints { make in
            make.top.equalTo(16*widthScale).priority(.high)
            make.left.equalTo(14*widthScale)
            make.bottom.equalTo(-1*widthScale)
            make.width.height.equalTo(34*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(logoImageView.snp.top).offset(-2*widthScale)
            make.height.equalTo(20*widthScale)
            make.left.equalTo(logoImageView.snp.right).offset(11*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.height.equalTo(15*widthScale)
            make.top.equalTo(titleLabel.snp.bottom).offset(2*widthScale)
        }
        
        rightButton.snp.makeConstraints { make in
            make.right.equalTo(-14*widthScale)
            make.top.equalTo(19*widthScale)
            make.height.equalTo(27*widthScale)
            make.width.equalTo(54*widthScale)
        }
    }
    
    @objc func rightButtonClick(_:UIButton) -> Void{
        self.didClickRightButton?(model!)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
